package com.itheima.reggie.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public R add(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");
    }

    @Override
    public R findpage(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> p = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (null != name) {
            wrapper.like(Setmeal::getName, name);
        }
        IPage<Setmeal> pageInfo = setmealMapper.selectPage(p, wrapper);
        //把setmeal转成setmealDto
        //把setmeal集合转成setmealDto集合
        List<Setmeal> setmeals = pageInfo.getRecords();
        String jsonString = JSON.toJSONString(setmeals);
        List<SetmealDto> setmealDtos = JSON.parseArray(jsonString, SetmealDto.class);

        //给setmealDto设置分类名称
        for (SetmealDto setmealDto : setmealDtos) {
            String categroyName = categoryMapper.selectById(setmealDto.getCategoryId()).getName();
            setmealDto.setCategoryName(categroyName);
        }
        //封装page对象
        String jsonString1 = JSON.toJSONString(pageInfo);
        Page page1 = JSON.parseObject(jsonString1, Page.class);
        page1.setRecords(setmealDtos);
        return R.success(page1);
    }

    @Override
    public void removeIds(Long[] ids) {
        setmealMapper.deleteBatchIds(Arrays.asList(ids));

        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
    }

    @Override
    public void modifyStatus(Long[] ids, Integer id) {
        Setmeal setmeal = new Setmeal();
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Setmeal::getStatus, id).in(Setmeal::getId, ids);
        setmealMapper.update(setmeal, wrapper);
    }

    @Override
    public R findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);


        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    @Override
    public void update(SetmealDto setmealDto) {
        setmealMapper.updateById(setmealDto);
        //先删后增
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishMapper.delete(wrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public R list(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        List<Setmeal> setmeals = setmealMapper.selectList(wrapper);
        return R.success(setmeals);
    }
}
